package com.devicecontrolrn

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import com.devicecontrolrn.Control.DevManageGuider
import com.devicecontrolrn.Control.SDKGuider
import com.facebook.react.bridge.Promise
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactContextBaseJavaModule
import com.facebook.react.bridge.ReactMethod
import com.hikvision.netsdk.HCNetSDK
import com.hikvision.netsdk.INT_PTR
import com.hikvision.netsdk.NET_DVR_JPEGPARA
import java.io.FileOutputStream
import java.io.IOException


class HKVisionDeviceModule(reactContext: ReactApplicationContext) : ReactContextBaseJavaModule(reactContext) {
    override fun getName(): String {
        return "HKVisionDeviceModule"
    }

    @ReactMethod
    fun login(devName:String, ip:String, port:String, userName:String, password: String, promise: Promise) {
        // clicked add button
        val deviceItem: DevManageGuider.DeviceItem = SDKGuider.g_sdkGuider.m_comDMGuider.DeviceItem()
        deviceItem.m_szDevName = devName
        deviceItem.m_struNetInfo = SDKGuider.g_sdkGuider.m_comDMGuider.DevNetInfo(
            ip,
            port,
            userName,
            password
        );
        if(SDKGuider.g_sdkGuider.m_comDMGuider.login_v40_jna(deviceItem.m_szDevName, deviceItem.m_struNetInfo)){
            promise.resolve(true);
        }
        else {
            val reason: Int = SDKGuider.g_sdkGuider.GetLastError_jni();
            // int 转为String
            promise.reject(reason.toString());
        }
    }

    @ReactMethod
    fun logout() {
        SDKGuider.g_sdkGuider.m_comDMGuider.login_v40_jna_with_index(0)
    }

    @ReactMethod
    fun snap(path: String, picQuality:Int, picSize: Int, bufferSize: Int,  promise: Promise) {
        // 设置抓拍参数
        val strJpeg = NET_DVR_JPEGPARA()
        strJpeg.wPicQuality = picQuality
        strJpeg.wPicSize = picSize
        val iBufferSize = bufferSize
        val sbuffer = ByteArray(iBufferSize)
        val bytesRerned = INT_PTR()
        if (SDKGuider.g_sdkGuider.m_comDMGuider.devList.size == 0) {
            promise.reject("未登录");
            return;
        }
        val flag: Boolean = HCNetSDK.getInstance()
            .NET_DVR_CaptureJPEGPicture_NEW(SDKGuider.g_sdkGuider.m_comDMGuider.devList.get(0).m_lUserID, 1, strJpeg, sbuffer, iBufferSize, bytesRerned)
        if (flag) {
            val bitmap = BitmapFactory.decodeByteArray(sbuffer, 0, sbuffer.size)
            // android保存bitmap到path
            try {
                val fos = FileOutputStream(path)
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos) // 使用JPEG格式，质量设置为100%
                fos.flush()
                fos.close()
                promise.resolve(path);
            } catch (e: IOException) {
                e.printStackTrace()
                promise.resolve(e.localizedMessage);
            }
        }
        else {
            promise.reject(HCNetSDK.getInstance().NET_DVR_GetLastError().toString());
        }
    }
}